package cn.youqu.happy.module.youqu.service.userFollows;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.UserFollowsCreateReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.UserFollowsExportReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.UserFollowsPageReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.UserFollowsUpdateReqVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsFollowReqVO;
import cn.youqu.happy.module.youqu.controller.app.userFollows.vo.AppUserFollowsUpdateReqVO;
import cn.youqu.happy.module.youqu.convert.userFollows.UserFollowsConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userFollows.UserFollowsDO;
import cn.youqu.happy.module.youqu.dal.mysql.userFollows.UserFollowsMapper;
import cn.youqu.happy.module.youqu.service.systemActivity.SystemActivityService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.youqu.happy.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.USER_FOLLOWS_ALREADY_FOLLOW;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.USER_FOLLOWS_NOT_EXISTS;

/**
 * 用户关注 Service 实现类
 *
 * @author 王其岩  20112001045
 */
@Service
@Validated
public class UserFollowsServiceImpl implements UserFollowsService {

    @Resource
    private UserFollowsMapper userFollowsMapper;
    @Resource
    @Lazy
    private SystemActivityService systemActivityService;

    @Override
    public Integer createUserFollows(UserFollowsCreateReqVO createReqVO) {
        // 插入
        UserFollowsDO userFollows = UserFollowsConvert.INSTANCE.convert(createReqVO);
        userFollowsMapper.insert(userFollows);
        // 返回
        return userFollows.getId();
    }

    @Override
    public void updateUserFollows(UserFollowsUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserFollowsExists(updateReqVO.getId());
        // 更新
        UserFollowsDO updateObj = UserFollowsConvert.INSTANCE.convert(updateReqVO);
        userFollowsMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserFollows(Integer id) {
        // 校验存在
        this.validateUserFollowsExists(id);
        // 删除
        userFollowsMapper.deleteById(id);
    }

    private void validateUserFollowsExists(Integer id) {
        if (userFollowsMapper.selectById(id) == null) {
            throw exception(USER_FOLLOWS_NOT_EXISTS);
        }
    }

    private void validateUserFollowsExists(Integer id, Long uid) {
        if (userFollowsMapper.findByIdAndUid(id, uid) == null) {
            throw exception(USER_FOLLOWS_NOT_EXISTS);
        }
    }


    @Override
    public UserFollowsDO getUserFollows(Integer id) {
        return userFollowsMapper.selectById(id);
    }

    @Override
    public List<UserFollowsDO> getUserFollowsList(Collection<Integer> ids) {
        return userFollowsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserFollowsDO> getUserFollowsPage(UserFollowsPageReqVO pageReqVO) {
        return userFollowsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserFollowsDO> getUserFollowsList(UserFollowsExportReqVO exportReqVO) {
        return userFollowsMapper.selectList(exportReqVO);
    }

    private List<UserFollowsDO> filterUserOwnUserFollows(List<UserFollowsDO> userFollowsDOS, Long uid) {
        return userFollowsDOS.stream().filter(item -> Objects.equals(item.getUid(), uid)).collect(Collectors.toList());
    }

    @Override
    public PageResult<UserFollowsDO> getUserFollowsPage(UserFollowsPageReqVO pageVO, Long loginUserId) {
        PageResult<UserFollowsDO> userFollowsDOPageResult = userFollowsMapper.selectPage(pageVO);
        List<UserFollowsDO> list = userFollowsDOPageResult.getList();
        List<UserFollowsDO> userFollowsDOS = filterUserOwnUserFollows(list, loginUserId);
        userFollowsDOPageResult.setList(userFollowsDOS);
        userFollowsDOPageResult.setTotal((long) userFollowsDOS.size());
        return userFollowsDOPageResult;
    }

    @Override
    public List<UserFollowsDO> getUserFollowsList(Collection<Integer> ids, Long loginUserId) {
        List<UserFollowsDO> userFollowsDOS = userFollowsMapper.selectBatchIds(ids);

        return filterUserOwnUserFollows(userFollowsDOS, loginUserId);
    }

    @Override
    public UserFollowsDO getUserFollows(Integer id, Long loginUserId) {
        return userFollowsMapper.findByIdAndUid(id, loginUserId);
    }

    @Override
    public void deleteUserFollows(Integer id, Long loginUserId) {
        this.validateUserFollowsExists(id, loginUserId);
        userFollowsMapper.deleteById(id);
    }

    @Override
    public Integer followerFollows(AppUserFollowsFollowReqVO followReqVO, Long loginUserId) {
        systemActivityService.validateSystemActivityExists(followReqVO.getSysActivityId());
        //校验是否关注
        UserFollowsDO userFollowsDO = userFollowsMapper.findOneBySysActivityIdAndUid(followReqVO.getSysActivityId(), loginUserId);
        if (userFollowsDO != null) {
            throw exception(USER_FOLLOWS_ALREADY_FOLLOW);
        }
        UserFollowsDO convert = UserFollowsConvert.INSTANCE.convert(followReqVO);
        //设置用户
        convert.setUid(loginUserId);
        userFollowsMapper.insert(convert);
        return convert.getId();
    }

    @Override
    public void updateUserFollows(AppUserFollowsUpdateReqVO updateReqVO, Long loginUserId) {
        this.validateUserFollowsExists(updateReqVO.getId(), loginUserId);
        UserFollowsDO updateObj = UserFollowsConvert.INSTANCE.convert(updateReqVO);
        userFollowsMapper.updateById(updateObj);
    }

}
